package com.zhuiYun.project.api.order.mapper;

import com.zhuiYun.project.api.order.entity.CommodityOrderVo;
import com.zhuiYun.project.api.order.entity.HandworkDiyOrderVo;
import com.zhuiYun.project.api.order.entity.OrderVO;
import com.zhuiYun.project.api.order.entity.OrdersVo;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderVoMapper extends BaseMapper<OrderVO> {

    @Select("SELECT o.id,c.id AS cid,c.commodity_id,o.out_trade_no,o.expiration_time,o.order_amount_total,o.order_number,o.create_time,o.order_type_id,st.`status`,u.address_consignee,u.address_location_province,u.address_location_county,u.address_detailed,u.address_telephone_shift,u.address_location_city,c.commodity_quantity,m.commodity_name,m.favorable_price,m.image_id,p.`mode` \n" +
            "            FROM orders o\n" +
            "            LEFT JOIN commodity_order c\n" +
            "            ON o.id=c.orders_id\n" +
            "            LEFT JOIN commodity m\n" +
            "            ON m.id= c.commodity_id\n" +
            "            LEFT JOIN order_status st\n" +
            "            on o.status_id = st.id\n" +
            "            LEFT JOIN user_address u\n" +
            "            on o.address_id = u.id\n" +
            "            LEFT JOIN pay_mode p\n" +
            "            ON o.pay_mode_id = p.id\n" +
            "            WHERE o.user_id=#{userId} and o.order_type_id= 1 and o.id=#{id} and o.del_flag=0 \n" +
            "            GROUP BY \n" +
            "            m.id,\n" +
            "            o.id, \n" +
            "            c.id,\n" +
            "            st.id, \n" +
            "            u.id;")

    // 查询用户商品信息（用于uniapp查询用户所有的订单信息）
    List<OrderVO> getMyCommodityAllOrder(@Param("id") Integer id, @Param("userId") Integer userId);

    @Select("select id,order_number,user_id,order_type_id from orders where user_id = #{userId} and del_flag=0")
    // 查询用户的总订单（用于uniapp查询用户所有的订单信息）
    List<OrderVO> getMyOrders(Integer userId);

    @Select("SELECT o.id,h.id AS hid,h.diy_id,o.order_amount_total,o.out_trade_no,o.expiration_time,o.order_number,o.create_time,o.order_type_id,h.handwork_diy_quantity,d.diy_name,d.diy_price,st.`status`,u.address_consignee,u.address_location_province,u.address_location_county,u.address_detailed,u.address_telephone_shift,u.address_location_city,d.diy_image_id,p.mode\n" +
            "FROM orders o\n" +
            "LEFT JOIN handwork_diy_order h\n" +
            "ON o.id=h.orders_id\n" +
            "LEFT JOIN handwork_diy d\n" +
            "ON d.id= h.diy_id\n" +
            "LEFT JOIN order_status st\n" +
            "on o.status_id = st.id\n" +
            "LEFT JOIN user_address u\n" +
            "on o.address_id = u.id\n" +
            "LEFT JOIN pay_mode p\n" +
            "ON o.pay_mode_id = p.id\n" +
            "WHERE o.user_id=#{userId} and o.order_type_id= 2 and o.id=#{id} and o.del_flag=0")
    @Results({
            @Result(column = "id",property = "id"),

    })
    // 查询diy商品信息（用于uniapp查询用户所有的订单信息）
    List<OrderVO> getMyDiyAllOrder(Integer id, @Param("userId") Integer userId);

    @Select("select id,order_number,user_id,order_type_id from orders where user_id = #{userId} and status_id = #{status}")
    @Results({
            @Result(column = "id",property = "id"),

    })
    // 查询点击的订单状态信息（用于uniapp订单状态栏）
    List<OrderVO> getMyStatusOders(Integer status, Integer userId);


    @Select("SELECT o.order_number,o.create_time,o.order_type_id,o.expiration_time,st.`status`,u.address_consignee,u.address_location_province,u.address_location_county,u.address_detailed,u.address_telephone_shift,u.address_location_city,c.commodity_quantity,m.commodity_name,m.favorable_price,m.image_id\n" +
            "            FROM orders o\n" +
            "            LEFT JOIN commodity_order c\n" +
            "            ON o.id=c.orders_id\n" +
            "            LEFT JOIN commodity m\n" +
            "            ON m.id= c.commodity_id\n" +
            "            LEFT JOIN order_status st\n" +
            "            on o.status_id = st.id\n" +
            "           LEFT JOIN user_address u\n" +
            "            on o.address_id = u.id\n" +
            "            WHERE o.user_id=#{userId} and o.order_type_id= 1 and st.id=#{status} and o.del_flag=0\n" +
            "            GROUP BY \n" +
            "            m.id,\n" +
            "            o.id, \n" +
            "            c.id,\n" +
            "            st.id, \n" +
            "            u.id;")
    @Results({
            @Result(column = "id",property = "id"),

    })
    // 查询用户对应订单状态的商品订单信息(用于uniapp状态栏)
    List<OrderVO> getMyStatusCommodityAllOrder(Integer id, Integer userId, Integer status);


    @Select("SELECT o.id,o.order_number,o.create_time,o.order_type_id,o.expiration_time,h.handwork_diy_quantity,d.diy_name,d.diy_price,st.`status`,u.address_consignee,u.address_location_province,u.address_location_county,u.address_detailed,u.address_telephone_shift,u.address_location_city,d.diy_image_id\n" +
            "FROM orders o\n" +
            "LEFT JOIN handwork_diy_order h\n" +
            "ON o.id=h.orders_id\n" +
            "LEFT JOIN handwork_diy d\n" +
            "ON d.id= h.diy_id\n" +
            "LEFT JOIN order_status st\n" +
            "on o.status_id = st.id\n" +
            "LEFT JOIN user_address u\n" +
            "on o.address_id = u.id\n" +
            "WHERE o.user_id=#{userId} and o.order_type_id= 2 and st.id = #{status};")
    @Results({
            @Result(column = "id",property = "id"),

    })
    // 查询用户对应订单状态的diy商品订单信息(用于uniapp状态栏)
    List<OrderVO> getMyStatusDiyAllOrder(Integer id, Integer userId, Integer status);


    @Select("SELECT o.id,o.order_number,o.order_amount_total,o.create_time,o.freight,s.`status`,p.`mode`,u.address_consignee,u.address_location_province,u.address_location_county,u.address_detailed,u.address_telephone_shift,u.address_location_city\n" +
            "FROM orders o\n" +
            "LEFT JOIN order_status s\n" +
            "ON o.status_id = s.id\n" +
            "LEFT JOIN pay_mode p\n" +
            "on o.pay_mode_id = p.id\n" +
            "LEFT JOIN user_address u\n" +
            "on o.address_id= u.id\n" +
            "WHERE o.user_id =#{userId} and o.id=#{id} and o.del_flag=0")
    // 查询用户对应订单商品的详细信息(用于pc端订单)
    List<OrderVO> getInformation(@Param("userId") Integer userId,@Param("id") Integer id);

    @Select("SELECT o.id,o.order_amount_total,o.order_number,c.commodity_id,c.commodity_quantity,m.commodity_name,m.image_id\n" +
            "FROM orders o\n" +
            "LEFT JOIN commodity_order c \n" +
            "ON o.id= c.orders_id\n" +
            "LEFT JOIN commodity m\n" +
            "ON m.id= c.commodity_id\n" +
            "WHERE o.id = #{id} and o.del_flag = 0")
    // 查询用户对应订单商品详细信息的多条信息(用于pc端订单)
    List<OrderVO> getInformationf(@Param("id") Integer id);

    @Select("SELECT o.id,o.order_number,o.order_amount_total,o.create_time,o.freight,s.`status`,p.`mode`,u.address_consignee,u.address_location_province,u.address_location_county,u.address_detailed,u.address_telephone_shift,u.address_location_city\n" +
            "FROM orders o\n" +
            "LEFT JOIN order_status s\n" +
            "ON o.status_id = s.id\n" +
            "LEFT JOIN pay_mode p\n" +
            "on o.pay_mode_id = p.id\n" +
            "LEFT JOIN user_address u\n" +
            "on o.address_id= u.id\n" +
            "WHERE o.user_id =#{userId} and o.id=#{id}  and o.del_flag=0")
    // 查询用户对应订单diy的详细信息(用于pc端订单)
    List<OrderVO> getDiyInformation(@Param("userId") Integer userId,@Param("id") Integer id);

    @Select("SELECT o.id,o.order_amount_total,o.order_number,h.diy_id,h.handwork_diy_quantity,d.diy_name,d.diy_image_id\n" +
            "FROM orders o\n" +
            "LEFT JOIN handwork_diy_order h\n" +
            "ON o.id= h.orders_id\n" +
            "LEFT JOIN handwork_diy d\n" +
            "ON d.id= h.diy_id\n" +
            "WHERE o.id=#{id} and o.del_flag=0")
    // 查询用户对应订单diy详细信息的多条信息(用于pc端订单)
    List<OrderVO> getDiyInformationf(@Param("id") Integer id);

    @Select("SELECT o.create_time,s.id,s.`status`\n" +
            "FROM orders o\n" +
            "LEFT JOIN order_status s\n" +
            "ON o.status_id = s.id\n" +
            "WHERE o.user_id = #{userId} and o.id = #{id} and o.del_flag=0")
    @Results({
            @Result(property = "id",column = "id"),

    })
    //查询用户订单步骤详情信息(用于pc端订单详细详细查询)
    OrderVO getStepList(Integer userId, Integer id);


    @Select("SELECT r.id,r.orders_retreat_take_notes,a.orders_type,a.orders_cause,a.orders_describe,o.create_time,o.order_number,o.order_type_id,s.status,cc.commodity_name\n" +
            "FROM orders_retreat r\n" +
            "LEFT JOIN orders_after_sales a\n" +
            "ON r.orders_after_sales_id = a.id\n" +
            "LEFT JOIN orders o\n" +
            "ON r.orders_id = o.id\n" +
            "LEFT JOIN orders_after_sales_status s\n" +
            "ON s.id = r.orders_status_id\n" +
            "LEFT JOIN commodity_order c\n" +
            "ON o.id = c.orders_id\n" +
            "LEFT JOIN commodity cc\n" +
            "ON c.commodity_id = cc.id\n" +
            "WHERE r.user_id= #{userId} and o.order_type_id =1 and r.del_flag = 0 and r.orders_retreat_take_notes = 1;")

    // 查询用户商品售后信息
    List<OrderVO> getMyCommodityOrderAfterSales(@Param("userId") Integer userId, @Param("status") Integer status);

    @Select("select id,order_number,user_id,order_type_id,status_id from orders where user_id = #{userId} and del_flag=0")
    // 查询用户所有商品售后信息
    List<OrderVO> getMyAllOrderAfterSales(Integer userId);

    @Select("SELECT r.id,r.orders_retreat_take_notes,a.orders_type,a.orders_cause,a.orders_describe,o.create_time,o.order_number,o.order_type_id,s.status,hh.diy_name\n" +
            "FROM orders_retreat r\n" +
            "LEFT JOIN orders_after_sales a\n" +
            "ON r.orders_after_sales_id = a.id\n" +
            "LEFT JOIN orders o\n" +
            "ON r.orders_id = o.id\n" +
            "LEFT JOIN orders_after_sales_status s\n" +
            "ON s.id = r.orders_status_id\n" +
            "LEFT JOIN handwork_diy_order h\n" +
            "ON o.id = h.orders_id\n" +
            "LEFT JOIN handwork_diy hh\n" +
            "ON h.diy_id= hh.id\n" +
            "WHERE r.user_id=#{userId}  and o.order_type_id =2 and r.del_flag = 0 and r.orders_retreat_take_notes = 1;")

    // 查询用户diy商品售后信息
    List<OrderVO> getMyDiyOrderAfterSales(@Param("userId") Integer userId);



    @Select("SELECT * FROM orders WHERE id = #{id}")
    int getId(Integer id);

    @Select("SELECT o.id,o.create_time,o.expiration_time,o.order_number,o.order_amount_total,o.order_type_id,o.out_trade_no,s.`status`,p.`mode`,a.address_consignee\n" +
            "FROM orders o \n" +
            "LEFT JOIN order_status s\n" +
            "ON o.status_id = s.id\n" +
            "LEFT JOIN pay_mode p\n" +
            "ON o.pay_mode_id = p.id\n" +
            "LEFT JOIN user_address a\n" +
            "on o.address_id = a.id\n" +
            "WHERE o.del_flag = 0 AND o.user_id = #{userId}")
    // 查询用户所有订单信息 总订单信息
    List<OrdersVo> getMyOrder(@Param("userId") Integer userId);

    @Select("SELECT c.id,cc.commodity_name,c.orders_id,cc.image_id,c.commodity_quantity,cc.id AS cid\n" +
            "FROM commodity_order c\n" +
            "LEFT JOIN commodity cc\n" +
            "ON c.commodity_id = cc.id\n" +
            "LEFT JOIN orders o\n" +
            "ON c.orders_id = o.id\n" +
            "WHERE c.del_flag= 0 and c.orders_id = #{id} and o.order_type_id= 1")
    //查询用户商品订单信息
    List<CommodityOrderVo> getMyCommodityAllOrderpc(@Param("id") Integer id);
    @Select("SELECT h.id,h.handwork_diy_quantity,h.orders_id,hh.diy_name,hh.diy_image_id,hh.id as hid\n" +
            "FROM handwork_diy_order h\n" +
            "LEFT JOIN handwork_diy hh\n" +
            "ON h.diy_id = hh.id\n" +
            "LEFT JOIN orders o\n" +
            "ON h.orders_id = o.id\n" +
            "WHERE h.del_flag = 0 and h.orders_id = #{id} and o.order_type_id= 2")
        //查询用户商品订单信息
    List<HandworkDiyOrderVo> getMyDiyAllOrderpc(@Param("id") Integer id);


    @Select("SELECT o.id,o.create_time,o.order_number,o.order_amount_total,o.order_type_id,o.out_trade_no,s.`status`,p.`mode`,a.address_consignee\n" +
            "FROM orders o \n" +
            "LEFT JOIN order_status s\n" +
            "ON o.status_id = s.id\n" +
            "LEFT JOIN pay_mode p\n" +
            "ON o.pay_mode_id = p.id\n" +
            "LEFT JOIN user_address a\n" +
            "on o.address_id = a.id\n" +
            "WHERE o.del_flag = 0 AND o.user_id = #{userId} and o.status_id = #{status}")
    // 查询点击的订单状态信息（用于uniapp订单状态栏）
    List<OrdersVo> getMyStatusOdersPc(Integer status, Integer userId);
}
